<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
  String jsessionid = session.getId();
%>

<div class="row">
    <div class="col-lg-12">
        <div class="panel">
            <div class="panel-heading">
                <h3 class="panel-title">公告通知登记表</h3>
            </div>

            <!-- BASIC FORM ELEMENTS -->
            <!--===================================================-->
            <form id="form_notice_input" class="panel-body form-horizontal form-padding">

                <input type="hidden" name="keyId" id="keyId" value="${notice.id}" />

                <div class="form-group">
                    <label class="col-md-2 control-label">公告主题<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <input type="text" id="subject" name="subject" class="form-control" placeholder="请输入主题" value="${notice.subject}">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">是否置顶<i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">

                        <div class="radio">
                            <!-- Inline radio buttons -->
                            <input type="radio" name="isTop" id="isTop1Selected" class="magic-radio" value="1"
                            <c:if test="${notice.isTop==1}"> checked="checked"</c:if> >
                            <label for="isTop1Selected">是</label>

                            <input type="radio" name="isTop" id="isTop2Selected" class="magic-radio" value="0"
                            <c:if test="${notice.isTop==0}"> checked="checked"</c:if> >
                            <label for="isTop2Selected">否</label>

                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">公告类型<i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <select id="dictItemId" name="dictItemId" class="form-control">
                            <option></option>
                            <c:forEach var="item" items="${dictItems}" varStatus="status">
                                <option value="${item.id}" <c:if test="${item.id == notice.dictItem.id}"> selected="selected"</c:if>>${item.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">
                        <a href="javascript:void(0);" onclick="fn_departments_dialog_select('n','deptName','deptId')" style="color: #0a6dbd">发布部门</a>
                        <i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <input type="text" id="deptName" name="deptName" class="form-control" placeholder="请选择发布部门" value="${notice.issueDepartment.name}">
                        <input type="hidden" id="deptId" name="deptId" value="${notice.issueDepartment.id}">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">通知对象<i class="fa fa-asterisk txt-color-red"></i>
                        <span id="noticeTypeSpan" style="display: none">${notice.noticeType+1}</span>
                    </label>
                    <div class="col-md-6">

                        <div class="radio">
                            <!-- Inline radio buttons -->
                            <input type="radio" name="noticeType" id="noticeType1Selected" class="magic-radio" value="0"
                            <c:if test="${notice.noticeType==0}"> checked="checked"</c:if> >
                            <label for="noticeType1Selected">全体员工</label>

                            <input type="radio" name="noticeType" id="noticeType2Selected" class="magic-radio" value="1"
                            <c:if test="${notice.noticeType==1}"> checked="checked"</c:if> >
                            <label for="noticeType2Selected">部门</label>

                            <input type="radio" name="noticeType" id="noticeType3Selected" class="magic-radio" value="2"
                            <c:if test="${notice.noticeType==2}"> checked="checked"</c:if> >
                            <label for="noticeType3Selected">个人</label>

                        </div>
                    </div>
                </div>

                <div class="form-group" id="divNoticeDepts">
                    <label class="col-md-2 control-label">
                        <a href="javascript:void(0);" onclick="fn_departments_dialog_select('y','deptNames','deptIds')" style="color: #0a6dbd">通知部门</a>
                        <i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <input type="text" id="deptNames" name="deptNames" class="form-control" placeholder="请选择通知部门"
                               value="<c:forEach var="item" items="${notice.noticeDepartments}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                        <input type="hidden" id="deptIds" name="deptIds"
                               value="<c:forEach var="item" items="${notice.noticeDepartments}" varStatus="status">${item.id}<c:if test="${!status.last}">,</c:if></c:forEach>">
                    </div>
                </div>

                <div class="form-group" id="divNoticeUsers">
                    <label class="col-md-2 control-label">
                        <a href="javascript:void(0);" onclick="fn_members_dialog_select()" style="color: #0a6dbd">通知个人</a>
                        <i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <input type="text" id="userNames" name="userNames" class="form-control" placeholder="请选择通知个人"
                               value="<c:forEach var="item" items="${notice.noticeUsers}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                        <input type="hidden" id="userIds" name="userIds"
                               value="<c:forEach var="item" items="${notice.noticeUsers}" varStatus="status">${item.id}<c:if test="${!status.last}">,</c:if></c:forEach>">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">内容<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <input type="text" id="content" name="content" class="form-control" placeholder="请输入内容" value="${notice.content}">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">附件上传</label>
                    <div class="col-md-6">
                        <input name="uploadify" id="fileAttachment" placeholder="" type="file" >
                        <input name="hiddenFiles" id="hiddenFilesAttachment" type="hidden" value="${notice.fileAttachment}">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">发布人</label>
                    <div class="col-md-2">
                        <p class="form-control-static">${notice.creater.name}</p>
                    </div>
                    <label class="col-md-2 control-label">发布日期</label>
                    <div class="col-md-2">
                        <p class="form-control-static"><fmt:formatDate pattern="yyyy-MM-dd" value="${notice.dateApply}" /></p>
                    </div>
                </div>
            </form>
            <!--===================================================-->
            <!-- END BASIC FORM ELEMENTS -->

            <div class="panel-footer">
                <a class="btn btn-default" id="apply-re-common" href="javascript:void(0)">
                    <i class="fa fa-lg fa-mail-reply"></i> 返回视图
                </a>
                <c:if test="${notice.numStatus==0 && user.id==notice.creater.id}">
                    <a class="btn btn-success" id="btn-save-common" href="javascript:void(0);"><i class="fa fa-lg fa-download"></i> 保存表单 </a>
                    <a class="btn btn-warning" id="btn-submit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 提交表单 </a>
                </c:if>
            </div>
        </div>
    </div>
</div>

<div id="divUserDepts" style="display: none">
    <c:forEach var="item" items="${userDepts}" varStatus="status"><div><input type="radio" name="dlg-radio" value="${item.id}" style="margin:10px" <c:if test="${status.first}">checked="checked"</c:if>>${item.name}</div></c:forEach>
</div>

<script>

    // 附件上传
    inputLoad({
        objId:"fileAttachment",
        entityName:"fileAttachment",
        sourceId:"hiddenFilesAttachment",
        jsessionid:"<%=jsessionid%>"
    });

    // 字典列表下拉框
    $("#dictItemId").select2({
        placeholder: "请选择一个公告类型",
        allowClear: true
    });

    // 部门选择框
    function fn_departments_dialog_select(isMulti,nameField,idField) {

        __open_dialog_select('选择部门', '<%=path%>/main/department/dialog?multi='+isMulti+'&deptIds='+ $('#'+idField).val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 获取 user
                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_departments_select(callback);
        });
    }

    function fn_members_dialog_select() {

        __open_dialog_select('选择成员', '<%=path%>/main/member/dialogs?sids='+$("#userIds").val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 获取 user
                    $('#userIds').val(data.ids);
                    $('#userNames').val(data.names)
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_members_select(callback);
        });
    }

    //校验
    function infoValidate() {
        var tmp = "";
        tmp = $("input#subject").val();
        if(!tmp){
            __toastr_error("请输入通知主题！");
            return false;
        }

        tmp = $("select#dictItemId").val();
        if(!tmp){
            __toastr_error("请选择公告类型！");
            return false;
        }

        tmp = $("input[name=noticeType]").filter(":checked").val();
        if(tmp=="1"){
            tmp = $("input#deptIds").val();
            if(!tmp){
                __toastr_error("请选择通知部门！");
                return false;
            }
        } else if(tmp=="2"){
            tmp = $("input#userIds").val();
            if(!tmp){
                __toastr_error("请选择通知个人！");
                return false;
            }
        }

        tmp = $("input#content").val();
        if(!tmp){
            __toastr_error("请输入内容！");
            return false;
        }

        return true;
    }

    //保存
    $("#btn-save-common").click(
            function(e) {
                __confirm_dialog("提示：","确定保存吗？",
                        function () {
                            __form_save("form_notice_input", "<%=path%>/activiti/notice/save");
                            returnToHashUrl();
                        }
                )
            }
    );

    //提交
    $("#btn-submit-common").click(
            function(e) {
                var $valid = infoValidate();
                if(!$valid) return false;

                __confirm_dialog("提示：","提交后填写资料不得修改，请确认是否进行提交？",
                        function () {
                            __form_save("form_notice_input", "<%=path%>/activiti/notice/submit");
                            returnToHashUrl();
                        }
                )
            }
    );

    // 完成修改
    $("#btn-update-common").click(
        function(e) {
            var $valid = infoValidate();
            if(!$valid) return false;

            __confirm_dialog("提示：","请确认是否完成修改？",
                function () {
                    __form_save("form_notice_input", "<%=path%>/activiti/notice/update");
                    returnToHashUrl();
                }
            )
        }
    );

    //返回视图
    $("#apply-re-common").click(returnToHashUrl);

    function selectUserDept(){
        BootstrapDialog.show({
            title: '请选择部门',
            message: $("#divUserDepts").html(),
            buttons: [{
                label: '确定',
                cssClass: 'btn-primary',
                action: function(dialog) {
                    // 判断是否选择了
                    var radio = $("input[name='dlg-radio']").filter(":checked");
                    var id = radio.val();
                    var name = radio.parent().text();
                    $("#deptId").val(id);
                    $("#deptName").val(name);
                    dialog.close();
                }
            }]
        });
    }

    // 加载完成后执行
    $(function(){
        // 如果用户在多个部门有duty,则弹框让他选择所在部门
        if(${userDepts.size() > 0}){
            selectUserDept();
        }

        $("#noticeType1Selected").click(function(){
            $("#divNoticeDepts").hide();
            $("#divNoticeUsers").hide();
        });

        $("#noticeType2Selected").click(function(){
            $("#divNoticeDepts").show();
            $("#divNoticeUsers").hide();
        });

        $("#noticeType3Selected").click(function(){
            $("#divNoticeDepts").hide();
            $("#divNoticeUsers").show();
        });

        var type = $("#noticeTypeSpan").text();
        $("#noticeType" + type + "Selected").click();
    })

</script>




